package com.sali.handler;

import cn.hutool.core.convert.Convert;
import cn.hutool.json.JSONObject;
import com.sali.enums.ProtocolHandleTypeEnum;
import com.sali.model.BaseFieldRule;
import com.sali.model.FieldModel;
import com.sali.model.ProtocolModel;
import com.sali.rule.Rule;
import org.apache.commons.lang3.StringUtils;

import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @description: 字段值依据规则转换
 * @author: sali
 * @date: 2024/7/16 21:29.
 * @version: 1.0
 */
public class FieldRuleHandle implements Function<BaseFieldRule, FieldModel> {


    private ProtocolModel protocolModel;
    private Object content;

    public FieldRuleHandle(ProtocolModel protocolModel, Object content) {
        this.protocolModel = protocolModel;
        this.content = content;
    }

    @Override
    public FieldModel apply(BaseFieldRule fieldRule) {
        ProtocolHandleTypeEnum handleType = protocolModel.getHandleType();
        /**
         * 从多个来源字段获取到的值列表
         */
        List<Object> srcValues;

        /**
         * 1、根据字段进行转换
         */
        switch (handleType) {
            case FILE_HANDLE_TYPE:
                srcValues = fieldConvertHandle(fieldRule, (JSONObject) content);
                break;
            default:
                srcValues = Collections.EMPTY_LIST;
                break;
        }

        /**
         * 处理规则
         * 1、把多个字段变成一个值
         * 2、调用规则转换程序进行值转换
         */
        Object value = handleMultiValue(srcValues);
        List<Rule> rules = fieldRule.getRules();
        if (rules != null) {
            for (Rule rule : rules) {
                value = rule.invoke(value);
            }
        }

        /**
         * 字段类型转换
         */
        try {
            String fieldType = fieldRule.getField().getFieldType();
            Class<?> aClass = Class.forName(fieldType);
            value = Convert.convert(aClass, value);
        } catch (ClassNotFoundException e) {
            value = null;
        }

        return new FieldModel(fieldRule.getField().getField(), value, fieldRule.getField().isMust());
    }

    /**
     * 字段转换处理
     *
     * @param fieldRule
     * @return
     */
    private List<Object> fieldConvertHandle(BaseFieldRule fieldRule, JSONObject content) {
        List<Object> srcValues;
        List<String> srcFields = fieldRule.getSrcFields();
        if (srcFields == null || srcFields.isEmpty()) {
            /**
             * 非必要字段
             */
            Object defaultValue = fieldRule.getField().getDefaultValue();
            srcValues = Collections.singletonList(defaultValue);
        } else {
            srcValues = srcFields.stream().map(content::get).filter(Objects::nonNull).collect(Collectors.toList());
            /**
             * 进行默认值初始化
             */
            if (srcValues.isEmpty()) {
                Object defaultValue = fieldRule.getField().getDefaultValue();
                srcValues.add(defaultValue);
            }
        }
        return srcValues;
    }

    /**
     * 从多个值中先取那个就使用那个
     *
     * @param values
     * @return
     */
    private Object handleMultiValue(List<Object> values) {
        for (Object value : values) {
            if (value != null && !StringUtils.isEmpty(String.valueOf(value))) {
                return value;
            }
        }
        return null;
    }

}
